package org.wjk.servicimpl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.wjk.annation.CachaUsing;
import org.wjk.mapper.SysUserMppr;
import org.wjk.pojo.entity.SysUserEntity;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class UserDetailServiceImpl implements UserDetailsService {
    private final SysUserMppr userMppr;
    @Override
    @CachaUsing(key = "CURRENT::USER")
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.debug("当前登录用户的用户名为{}", username);
        /*第一步：查询对应的用户信息*/
        SysUserEntity user = userMppr.loadUserByUsername(username);
        /*第二步：校验该用户名对应的用户信息是否存在*/
        if(user == null)
            throw new  UsernameNotFoundException("用户名不存在，请求查证！");
        List<String> permissions = userMppr.queryCurrentUserHasPermissions(username);
        user.setPermissions(permissions);
        log.debug("当前登录用户信息为{}", user);
        return user;
    }
}
